package com.younion.action.manage;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.business.IAfficheService;
import com.younion.business.IRegioninfoService;
import com.younion.common.base.BaseAction;
import com.younion.common.constants.SysConstants;
import com.younion.common.paginator.Paginator;
import com.younion.util.UUIDUtil;
import com.younion.vo.AfficheVO;
import com.younion.vo.RegioninfoVO;
import com.younion.vo.UserVO;

import cpcns.jhsks.util.MapCollectionUtils;

public class AfficheAction extends BaseAction {
	
	private static final long serialVersionUID = -6479683774471334573L;

	protected static final Log logger = LogFactory.getLog(AfficheAction.class);
	
	private IAfficheService afficheService = null;
	
	private IRegioninfoService regioninfoService = null;
	
	private AfficheVO afficheVO = new AfficheVO();
	
	private List<AfficheVO> afficheList;
	
	private Collection<RegioninfoVO> regioninfoList;
	
	private String ids;
	/**
	 * 列表：翻页区；数据区
	 */
	@RequiresPermissions("listAffiche")
	public String listAffiche() {
		try {
			Map paramMap = null;
			if (afficheVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(afficheVO));
			}
			int total = this.afficheService.getCountByMap(paramMap);
			Paginator paginator = this.getPaginator(total);
			this.afficheList = this.afficheService.get(paramMap, paginator);
			this.request.setAttribute("afficheList", afficheList);
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(2, "公告列表", "查询公告列表");
		return SUCCESS;
	}
	/**
	 * 新增
	 */
	@RequiresPermissions("addAffiche")
	public String addAffiche() {
		try {
			this.regioninfoList = this.regioninfoService.getAllProvince();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveAffiche")
	public String saveAffiche() {
		UserVO user = (UserVO) request.getSession().getAttribute(SysConstants.SESSION_USER);
		try {
			if (this.afficheVO.getId() == null){
				this.afficheVO.setUuid(new UUIDUtil().getUuid().toString());
				this.afficheVO.setUserVO(user);
				this.afficheService.save(this.afficheVO);
				this.addLog(3, "添加公告："+this.afficheVO.getTitle(), "添加公告");
			} else {
				Map<String, Integer> map = new HashMap<String, Integer>();
				map.put("id", this.afficheVO.getId());
				AfficheVO vo = this.afficheService.selectAfficheById(map);
				vo.setTitle(this.afficheVO.getTitle());
				vo.setTitleShow(this.afficheVO.getTitleShow());
				vo.setMode(this.afficheVO.getMode());
				vo.setContent(this.afficheVO.getContent());
				vo.setBound(this.afficheVO.getBound());
				vo.setArea(this.afficheVO.getArea());
				vo.setUpdateUserId(user.getId());
				Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(vo));
				this.afficheService.updateAfficheByMap(paramMap);
				this.addLog(4, "修改公告："+this.afficheVO.getTitle(), "ID为"+this.afficheVO.getId());
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		request.setAttribute("refreshNavTabId", "afficheList");
		return SUCCESS;
	}
	/**
	 *修改保存，调度Jsp
	 */
	@RequiresPermissions("editAffiche")
	public String editAffiche() {
		try {
			Map paramMap = null;
			if (afficheVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(afficheVO));
			}
			this.afficheVO = this.afficheService.selectAfficheById(paramMap);
			this.regioninfoList = this.regioninfoService.getAllProvince();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	/**
	 * 预览
	 * @return
	 */
	@RequiresPermissions("previewAffiche")
	public String previewAffiche() {
		try {
			Map paramMap = null;
			if (afficheVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(afficheVO));
			}
			this.afficheVO = this.afficheService.selectAfficheById(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 */
	@RequiresPermissions("deleteAffiche")
	public String deleteAffiche() {
		try {
			Map<String, Integer> paramMap = new HashMap<String, Integer>();
			paramMap.put("id", Integer.parseInt(this.ids));
			this.afficheService.deleteAfficheById(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(5, "删除公告", "ID为"+this.ids);
		request.setAttribute("refreshNavTabId", "afficheList");
		return SUCCESS;
	}
	/**
	 * 批量删除
	 */
	@RequiresPermissions("deleteAffiches")
	public String deleteAffiches() {
		try {
			List list = Arrays.asList(this.ids.split(","));
			this.afficheService.deleteAfficheByIds(list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		this.addLog(5, "批量删除公告", "ID为"+this.ids);
		request.setAttribute("refreshNavTabId", "afficheList");
		return SUCCESS;
	}
	
	public String listWebAffiche() {
		String temp = request.getParameter("page");
		Integer page = 1;
		if (temp != null && !temp.trim().equals("")) {
			try {
				page = Integer.parseInt(temp);
			} catch (Exception ex) {
				page = 1;
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mode", 2);
		try {
			int total = this.afficheService.getCountByMap(map);
			Paginator paginator = this.getPaginator(total);
			paginator.setPageIndex(page);
			paginator.setPageSize(14);
			this.afficheList = this.afficheService.selectWebAffiche(map, paginator);
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
	
	public AfficheVO getAfficheVO() {
		return this.afficheVO;
	}

	public void setAfficheVO(AfficheVO afficheVO) {
		this.afficheVO = afficheVO;
	}

	public void setAfficheService(IAfficheService afficheService) {
		this.afficheService = afficheService;
	}
	
	public void setRegioninfoService(IRegioninfoService regioninfoService) {
		this.regioninfoService = regioninfoService;
	}
	
	public Collection<RegioninfoVO> getRegioninfoList() {
		return this.regioninfoList;
	}
	
	public List<AfficheVO> getAfficheList() {
		return this.afficheList;
	}
	
	public void setIds(String ids) {
		this.ids = ids;
	}

}
